import 'package:flutter/material.dart';
import 'package:hxdj/common/components/my_clip_rect.dart';
import 'package:hxdj/common/components/my_smart_refresher.dart';
import 'package:hxdj/common/components/my_text.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:hxdj/message/message_page/message_viewmodel.dart';
import 'package:hxdj/message/sys_message/sys_message_viewmodel.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

//系统消息
class SysMessagePage extends StatefulWidget {
  static String routeName = "/SysMessagePage";
  final String messageId;

  const SysMessagePage({super.key, required this.messageId});

  @override
  State<SysMessagePage> createState() => _SysMessagePageState();
}

class _SysMessagePageState extends State<SysMessagePage> {
  //viewModel
  SysMessageViewModel? viewModel;

  late ScrollController? scrollController;

  late RefreshController? refreshController;
  @override
  void initState() {
    super.initState();
    scrollController = ScrollController();
    refreshController = RefreshController();
    debugPrint("messageId:${widget.messageId}");
  }

  @override
  Widget build(BuildContext context) {
    return BasePage<SysMessageViewModel>(
        onModelReady: (model) async {
          // 页面初始化完成后的回调
          viewModel = model;
          model.requestSysMessage(isFrist: true);
        },
        viewModel: SysMessageViewModel(widget.messageId),
        builder: (BuildContext context, SysMessageViewModel model, Widget? child) {
          //刷新完成
          refreshController!.refreshToIdle();
          refreshController!.loadComplete();

          if (!model.isHave) {
            //无更多数据
            refreshController!.loadNoData();
          }

          return Scaffold(
            backgroundColor: MyColours.bgColor,
            appBar: const MyAppBar(
              title: "系统消息",
            ),
            body: MySmartRefresher(
              dataList: model.dataList,
              enablePullUp: true,
              enablePullDown: true,
              noMoreData: !model.isHave,
              
              scrollController: scrollController!,

              itemBuilder: (BuildContext context, int index) {
                return SysMessageItem(messageModel: model.dataList[index]);
              },
              controller: refreshController!,
              onLoading: () {
                viewModel!.requestSysMessage();
              },
              onRefresh: () {
                //下拉刷新
                refreshController!.loadComplete;
                viewModel!.requestSysMessage(isFrist: true);
              },
            ),
          );
        });
  }
}

class SysMessageItem extends StatelessWidget {
  final SysMessageModel messageModel;

  const SysMessageItem({super.key, required this.messageModel});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10.w, right: 17.w),
      child: Column(
        children: [
          //时间
          Container(
            padding: EdgeInsets.only(top: 20.h, bottom: 17.h),
            child: MyText(CommonPlugin.messageTime(messageModel.createTime! * 1000),
                color: MyColours.color888888, fontSize: 12.sp),
          ),

          //消息
          Row(crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.start, children: [
            //头像
            MyClipRect(
              imageUrl: messageModel.img ?? "",
              width: 40.w,
              height: 40.w,
            ),

            Expanded(
              child: Container(
                  padding: EdgeInsets.only(left: 10.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      //昵称
                      Container(
                        padding: EdgeInsets.only(left: 10.w, bottom: 8.h, top: 2.h),
                        child: MyText(messageModel.name ?? "", color: MyColours.color888888, fontSize: 12.sp),
                      ),

                      //消息体
                      MyBorder(
                          radius: 12.w,
                          color: MyColours.materialBg,
                          child: Container(
                              padding: const EdgeInsets.only(left: 12, right: 25, top: 12, bottom: 12),
                              child: Text(
                                messageModel.content ?? "",
                                maxLines: 4,
                              ))),
                    ],
                  )),
            ),
          ]),
        ],
      ),
    );
  }
}
